﻿using System;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;
using System.Xml;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;

namespace YArchitech.HVAC
{
	public partial class FrmCountSel : System.Windows.Forms.Form
	{
		public FrmCountSel()
		{
			this.InitializeComponent();
		}

		public FrmCountSel(ExternalCommandData cmdData, string msg, ElementSet elements)
		{
			this.InitializeComponent();
			this.cmdDataForm = cmdData;
			this.msgForm = msg;
			this.elementsForm = elements;
			base.ShowInTaskbar = false;
		}

		private void BindData()
		{
			this.dataGridView1.RowHeadersVisible = false;
			this.dataGridView1.ColumnHeadersVisible = false;
			DataGridViewImageColumn dataGridViewImageColumn = new DataGridViewImageColumn();
			dataGridViewImageColumn.Name = "DropDown";
			dataGridViewImageColumn.HeaderText = "下拉";
			dataGridViewImageColumn.Width = 24;
			this.dataGridView1.Columns.Insert(0, dataGridViewImageColumn);
			DataGridViewImageColumn dataGridViewImageColumn2 = new DataGridViewImageColumn();
			dataGridViewImageColumn2.Name = "selLock";
			dataGridViewImageColumn2.HeaderText = "锁";
			dataGridViewImageColumn2.Width = 20;
			this.dataGridView1.Columns.Insert(0, dataGridViewImageColumn2);
			DataGridViewImageColumn dataGridViewImageColumn3 = new DataGridViewImageColumn();
			dataGridViewImageColumn3.Name = "selShow";
			dataGridViewImageColumn3.HeaderText = "显";
			dataGridViewImageColumn3.Width = 20;
			this.dataGridView1.Columns.Insert(0, dataGridViewImageColumn3);
			DataGridViewTextBoxColumn dataGridViewTextBoxColumn = new DataGridViewTextBoxColumn();
			dataGridViewTextBoxColumn.Name = "selName";
			dataGridViewTextBoxColumn.HeaderText = "选择集名称";
			dataGridViewTextBoxColumn.Width = 111;
			this.dataGridView1.Columns.Insert(0, dataGridViewTextBoxColumn);
			DataGridViewCheckBoxColumn dataGridViewCheckBoxColumn = new DataGridViewCheckBoxColumn();
			dataGridViewCheckBoxColumn.Name = "isCheck";
			dataGridViewCheckBoxColumn.HeaderText = "选";
			dataGridViewCheckBoxColumn.Width = 20;
			this.dataGridView1.Columns.Insert(0, dataGridViewCheckBoxColumn);
			this.GetXmlDoc();
			XmlNodeList xmlNodeList = this.GetXmlDoc().SelectNodes("Elements/Select");
			this.dataGridView1.Rows.Clear();
			if (xmlNodeList.Count == 0)
			{
				base.Hide();
				return;
			}
			this.dataGridView1.Rows.Add(xmlNodeList.Count);
			for (int i = 0; i < xmlNodeList.Count; i++)
			{
				if (xmlNodeList.Item(i).Attributes["Check"].Value == "true")
				{
					this.dataGridView1.Rows[i].Cells[0].Value = true;
				}
				else
				{
					this.dataGridView1.Rows[i].Cells[0].Value = false;
				}
				this.dataGridView1.Rows[i].Cells[1].Value = xmlNodeList.Item(i).Attributes["Name"].Value;
				if (xmlNodeList.Item(i).Attributes["Show"].Value == "true")
				{
					this.dataGridView1.Rows[i].Cells[2].Value = this.imageList1.Images[0];
				}
				else
				{
					this.dataGridView1.Rows[i].Cells[2].Value = this.imageList1.Images[1];
				}
				if (xmlNodeList.Item(i).Attributes["Lock"].Value == "true")
				{
					this.dataGridView1.Rows[i].Cells[3].Value = this.imageList1.Images[2];
				}
				else
				{
					this.dataGridView1.Rows[i].Cells[3].Value = this.imageList1.Images[3];
				}
				if (i == 0)
				{
					this.dataGridView1.Rows[i].Cells[4].Value = this.imageList1.Images[4];
				}
				else
				{
					this.dataGridView1.Rows[i].Cells[4].Value = this.imageList1.Images[5];
				}
			}
			this.dataGridView1.Width = 195;
			this.dataGridView1.Height = xmlNodeList.Count * 23;
			this.panel1.Width = 200;
			this.panel1.Height = this.dataGridView1.Height + 2;
			base.Width = this.panel1.Width;
			base.Height = this.panel1.Height;
			this.textBox1.Focus();
		}

		private void FrmCountSel_Load(object sender, EventArgs e)
		{
			this.BindData();
			this.textBox1.Focus();
		}

		private XmlDocument GetXmlDoc()
		{
			XmlDocument xmlDocument = new XmlDocument();
			string pathName = this.cmdDataForm.Application.ActiveUIDocument.Document.PathName;
			string title = this.cmdDataForm.Application.ActiveUIDocument.Document.Title;
			xmlDocument.LoadXml(XmlInfo.GetSelSet(pathName, title));
			return xmlDocument;
		}

		private void panel1_Paint(object sender, PaintEventArgs e)
		{
			WinFormTools.SetPanelColor(this.panel1, e);
		}

		private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
		{
			try
			{
				this.currentTxt = this.dataGridView1.Rows[e.RowIndex].Cells[1].Value.ToString();
				if (e.ColumnIndex == 4 && e.RowIndex == 0)
				{
					this.FrmParentMethod();
					base.Hide();
				}
				else
				{
					XmlDocument xmlDoc = this.GetXmlDoc();
					XmlNodeList xmlNodeList = xmlDoc.SelectNodes("Elements/Select");
					if (e.ColumnIndex == 0)
					{
						if (xmlNodeList.Item(e.RowIndex).Attributes["Check"].Value == "true")
						{
							xmlNodeList.Item(e.RowIndex).Attributes["Check"].Value = "false";
						}
						else
						{
							xmlNodeList.Item(e.RowIndex).Attributes["Check"].Value = "true";
							CmdTools.cmdSelect(this.cmdDataForm, this.dataGridView1.Rows[e.RowIndex].Cells[1].Value.ToString());
						}
					}
					if (e.ColumnIndex == 2)
					{
						if (xmlNodeList.Item(e.RowIndex).Attributes["Show"].Value == "true")
						{
							this.dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Value = this.imageList1.Images[1];
							xmlNodeList.Item(e.RowIndex).Attributes["Show"].Value = "false";
							CmdTools.cmdShow(this.cmdDataForm, this.dataGridView1.Rows[e.RowIndex].Cells[1].Value.ToString(), false);
						}
						else
						{
							this.dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Value = this.imageList1.Images[0];
							xmlNodeList.Item(e.RowIndex).Attributes["Show"].Value = "true";
							CmdTools.cmdShow(this.cmdDataForm, this.dataGridView1.Rows[e.RowIndex].Cells[1].Value.ToString(), true);
						}
					}
					if (e.ColumnIndex == 3)
					{
						if (xmlNodeList.Item(e.RowIndex).Attributes["Lock"].Value == "true")
						{
							this.dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Value = this.imageList1.Images[3];
							xmlNodeList.Item(e.RowIndex).Attributes["Lock"].Value = "false";
							CmdTools.cmdLock(this.cmdDataForm, this.dataGridView1.Rows[e.RowIndex].Cells[1].Value.ToString(), false);
						}
						else
						{
							this.dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Value = this.imageList1.Images[2];
							xmlNodeList.Item(e.RowIndex).Attributes["Lock"].Value = "true";
							CmdTools.cmdLock(this.cmdDataForm, this.dataGridView1.Rows[e.RowIndex].Cells[1].Value.ToString(), true);
						}
					}
					string pathName = this.cmdDataForm.Application.ActiveUIDocument.Document.PathName;
					string title = this.cmdDataForm.Application.ActiveUIDocument.Document.Title;
					XmlInfo.AddSelSet(pathName, title, xmlDoc.InnerXml);
				}
			}
			catch
			{
			}
		}

		private void FrmParentMethod()
		{
			if (this.classSel != null)
			{
				this.classSel();
			}
		}

		private void dataGridView1_CellEndEdit(object sender, DataGridViewCellEventArgs e)
		{
			if (e.ColumnIndex == 1)
			{
				XmlDocument xmlDoc = this.GetXmlDoc();
				if (xmlDoc.SelectSingleNode("Elements/Select[@Name='" + this.dataGridView1.Rows[e.RowIndex].Cells[1].Value.ToString() + "']") != null)
				{
					MessageBox.Show("选择集名称已经存在！");
					this.dataGridView1.Rows[e.RowIndex].Cells[1].Value = this.currentTxt;
					return;
				}
				xmlDoc.SelectNodes("Elements/Select").Item(e.RowIndex).Attributes["Name"].Value = this.dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Value.ToString();
				string pathName = this.cmdDataForm.Application.ActiveUIDocument.Document.PathName;
				string title = this.cmdDataForm.Application.ActiveUIDocument.Document.Title;
				XmlInfo.AddSelSet(pathName, title, xmlDoc.InnerXml);
			}
		}

		private ExternalCommandData cmdDataForm;

		private string msgForm;

		private ElementSet elementsForm = new ElementSet();

		public FrmCountSel.delegateSel classSel;

		private string currentTxt = "";

		public delegate void delegateSel();
	}
}
